package com.easondongh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.domain.SysDept;
import com.easondongh.system.mapper.SysDeptMapper;
import com.easondongh.system.mapper.SysRoleMapper;
import com.easondongh.system.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.system.mapper.SysUserMapper;
import com.easondongh.system.domain.SysUser;
import com.easondongh.system.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author EasonDongH
 * @date 2020/4/28 16:07
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public SysUser queryUserByLoginName(String loginName) {
        if(StringUtils.isBlank(loginName)) {
            log.warn("登录用户名不能为空");
            return null;
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysUser.COL_LOGIN_NAME, loginName);
        SysUser sysUser = this.userMapper.selectOne(queryWrapper);
        return sysUser;
    }

    @Override
    public PageResult<SysUser> getByPage(BasePageQueryVo<UserQueryVo> queryCondition) {
        IPage<SysUser> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        UserQueryVo queryParams = queryCondition.getQueryParams();
        if(queryParams != null) {
        }
        IPage<SysUser> iPage = this.userMapper.selectPage(page, queryWrapper);
        List<SysUser> records = iPage.getRecords();
        if(records != null && records.size() > 0) {
            List<SysDept> depts = this.deptMapper.selectList(null);
            if(depts != null && depts.size() > 0) {
                Map<Integer, String> map = depts.stream().collect(Collectors.toMap(SysDept::getId, SysDept::getTitle));
                for (SysUser record : records) {
                    record.setDeptName(map.get(record.getDeptId()));
                }
            }
        }
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), records);
    }

    @Override
    @Transactional
    public void dispatchRole(UserDispatchVo dispatchVo) {
        this.roleMapper.removeRidByUid(dispatchVo.getUid());
        List<Integer> rids = dispatchVo.getRids();
        if(rids != null && rids.size() > 0) {
            this.userMapper.insertRoles(dispatchVo.getUid(), rids);
        }
        return;
    }

    @Override
    public List<Integer> getRoles(Integer uid) {
        return this.userMapper.selectRidByUid(uid);
    }

    @Override
    @Transactional
    public boolean removeById(Integer id) {
        this.roleMapper.removeRidByUid(id);
        this.userMapper.deleteById(id);
        return true;
    }

    @Override
    public Integer getMaxOrderNum() {
        Integer orderNum = this.userMapper.getMaxOrderNum();
        return orderNum == null? 0: orderNum;
    }

    @Override
    public void updateAvatar(Integer id, String url) {
        this.userMapper.updateAvatar(id, url);
    }
}
